<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
	<title></title>
	<link id='icon' rel="shortcut icon" href="/web/mdl.png">

	<script type="text/javascript">
		var data = {}
		var request_sent = false
		function _send_request(_addr, _app, _data) {
			var _form = document.createElement('form')
			_form.style.visibility = 'hidden'
			_form.method = 'POST'
			_form.action = _addr+'/'+_app
			for ([_k,_v] of Object.entries(_data)) {
				var _input = document.createElement('input')
				_input.name = _k
				_input.value = JSON.stringify(_v)
				_form.appendChild(_input)
			}
			document.body.appendChild(_form)
			_form.submit()
		}
		function send_request() {
			if (! request_sent) {
				request_sent = true
				_send_request('', data['app'], {'data':data})
				setTimeout(()=>{request_sent=false}, 1000)
			}
		}
		function clear_request() {
			data['app_request'] = undefined
			data['prompt_request'] = undefined
			data['key_request'] = undefined
			data['mouse_request'] = undefined
		}
		function init() {
			if (data['stderr'])
				console.error(data['stderr'])
			if (data['key_request'] == 'true')
				document.onkeypress = function(e) {
					data['char_code'] = ''+e.charCode
					data['key_char'] = String.fromCharCode(e.charCode)
					clear_request()
					send_request()
				}
			if (data['mouse_request'] == 'true')
				document.onclick = function(e) {
					data['mouse_x'] = ''+e.x
					data['mouse_y'] = ''+e.y
					clear_request()
					send_request()
				}
			if (data['prompt_request']) {
				function show_prompt() {
					data['prompt_response'] = prompt(data['prompt_request'])
					if (data['prompt_response']===null)
						data['prompt_response'] = undefined
					clear_request()
					send_request()
				}
				setTimeout(show_prompt, 200)
			}
			if (data['app_request']) {
				var d = data['app_request'].split(' ')
				var delay = parseInt(d[0])
				var app = d[1]
				function goto_app() {
					data['app'] = app
					clear_request()		
					send_request()
				}
				setTimeout(goto_app, delay)
			}
		}
	</script>
</head>
<body></body>
</html>